﻿//==================================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 作 者：江怀玉
// Email:iSunStudio@hotmail.com
// 日 期：2011/10/11
// 描 述：常用反射方法,用于动态创建实体类，以及动态获取/设置实体对象的属性值
// 版 本：1.00
// 
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// V1.01  2011/10/28   贺兴祥            功能菜单选项      
//    
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.BLL.Base;
using iSun.Model;
using System.Web.UI.WebControls;
using System.Web;
using iSun.Entity;
using iSun.BLL;


namespace iSun.Web.UI.Controls
{
    public class ListMenu : Literal
    {
        //<div title="培训发展" style="padding: 0px;">
        //        <ul class="easyui-tree">
        //            <li state="closed"><span>培训体系</span>
        //                <ul>
        //                    <li><span><a onclick="addTabs('培训课程','form/test2.html')">培训课程</a></span></li>
        //                    <li><span><a onclick="addTabs('培训资料','form/test2.html')">培训资料</a></span></li>
        //                </ul>
        //            </li>
        //            <li state="closed"><span>需求征集</span>
        //                <ul>
        //                    <li><span><a onclick="addTabs('培训需求征集','form/test2.html')">培训需求征集</a></span></li>
        //                    <li><span><a onclick="addTabs('培训需求查询','form/test2.html')">培训需求查询</a></span></li>
        //                </ul>
        //            </li>
        //        </ul>
        //    </div>
        /// <summary>
        /// 生成菜单
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!DesignMode)
            {
                BaseBussnies bll = new BaseBussnies(SysFunction.New());
                bll.Order = "FunctionPath";
                //IList<SysFunction> list = bll.GetEntitys<SysFunction>("FunctionPath<>'@001'");
                BUser buser = new BUser(); BasePage bp = new BasePage();
                IList<SysFunction> list = buser.GetFunctionByUserId((Guid)bp.UserId);
                if (list != null && list.Count > 0)
                {
                    //功能栏目
                    IList<SysFunction> master = list.Where(p => p.PathCode.Length == 8).ToList();
                    string strDiv = string.Empty;
                    foreach (var e in master)
                    {
                        strDiv += "<div title=\"" + e.Name + "\" style=\"padding:0px\">" +
                                    "<ul class=\"easyui-tree\" id=\"leftTree\">";
                        //功能明细
                        var detail = list.Where(p => p.PathCode.Contains(e.PathCode)).OrderBy(p => p.PathCode).ToList();
                        {
                            strDiv += Menu(detail, e.ID);
                        }
                        strDiv += "</ul></div>";
                    }
                    writer.Write(strDiv);
                }
            }
            else
            {
                writer.Write("功能菜单");
            }
        }
        private string Menu(IList<SysFunction> ListSysFunction, Guid FunctionId)
        {
            string strMenu = string.Empty;
            foreach (var v in ListSysFunction)
            {
                if (v.ParentID == FunctionId)
                {
                    if (ListSysFunction.Where(p => p.ParentID == v.ID).Count() > 0)
                    {
                        strMenu += "<li state=\"closed\" ><span >" + v.Name + "</span><ul>" +
                                    Menu(ListSysFunction, v.ID) +
                                   "</ul></li>";
                    }
                    else
                    {
                        strMenu += "<li ><span><a  onclick=\"addTabs('" + v.Name + "','" + v.URL + ("&functionID=" + v.ID) + "')\">" + v.Name + "</a></span></li>";
                    }
                }
            }
            return strMenu;
        }
    }

    public class DataList : Literal
    {
        public string EntityName
        {
            get
            {
                return (this.Page as ListPage).GetStringFromQueryString("entity", "");
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!DesignMode)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                IList<DataListTemplate> list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
                string _strHtml = "<th field=\"{0}\" width=\"{2}px\">{1}</th>";
                if (list == null)
                {
                    writer.Write(string.Format(_strHtml, "Not Data", "Not Data", 400));
                    return;
                }
                //string _strHtml = "<th field=\"{0}\" width=\"{2}px\">{1}</th>";
                StringBuilder _sbHtml = new StringBuilder("");
                var _list = list.OrderBy(p => p.iOrder);
                foreach (DataListTemplate _template in _list)
                {
                    _sbHtml.Append(string.Format(_strHtml, _template.Field, _template.THCaption, _template.Width));
                }
                writer.Write(_sbHtml.ToString());
            }
            else
            {
                writer.Write("数据列表");
            }
        }
    }

    public class DataFields : Literal
    {
        public string EntityName
        {
            get
            {
                return (this.Page as ListPage).GetStringFromQueryString("entity", "");
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!DesignMode)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                IList<DataListTemplate> list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
                string _strHtml = @"{key:""{0}"",label:""{1}"",sortable:true}";
                if (list == null)
                {
                    writer.Write(string.Format(_strHtml, "Not Data", "Not Data", 400));
                    return;
                }
                StringBuilder _sbHtml = new StringBuilder("");
                var _list = list.OrderBy(p => p.iOrder);
                foreach (DataListTemplate _template in _list)
                {
                    _sbHtml.Append(string.Format(_strHtml, _template.Field, _template.THCaption));
                }
                writer.Write(_sbHtml.ToString());
            }
            else
            {
                writer.Write("数据列表");
            }
        }
    }

    public class DataWindow : Literal
    {
        public string EntityName
        {
            get
            {
                return (this.Page as ListPage).GetStringFromQueryString("entity", "");
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!DesignMode)
            {
                DataEditTemplate template = DataEditTemplate.CreateInstance();
                BaseBussnies<DataEditTemplate> bll = new BaseBussnies<DataEditTemplate>(template);
                template = bll.GetEntityByCondition<DataEditTemplate>(" EntityName='" + EntityName + "'");
                if (template == null)
                {
                    writer.Write("没有相关数据");
                    return;
                }
                writer.Write(HttpUtility.UrlDecode(template.HtmlContent));
            }
            else
            {
                writer.Write("数据列表");
            }
        }
    }


    public class UserInfo : Literal
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!DesignMode)
            {
                //Name;
                if (this.Page.User.Identity.IsAuthenticated)
                {
                    writer.Write(string.Format("您好：{0}", this.Page.User.Identity.Name));
                }
                else
                {
                    writer.Write("长时间不进行操作，用户信息已失效！");
                }
                base.Render(writer);
            }
            else
            {
                writer.Write("用户信息");
                base.Render(writer);
            }
        }
    }
}

